/*
  学习目标：

*/

import classNames from "classnames";
import { useEffect, useRef } from "react";
import styles from "./index.module.scss";
interface ITextAreaProps
  extends React.DetailedHTMLProps<
    React.TextareaHTMLAttributes<HTMLTextAreaElement>,
    HTMLTextAreaElement
  > {
  value?: string;
}

export default function Textarea({
  className,
  value,
  maxLength = 100,
  ...restProps
}: ITextAreaProps) {
  // 1.
  const textRef = useRef<HTMLTextAreaElement>(null);

  useEffect(() => {
    textRef.current?.focus();
    textRef.current?.setSelectionRange(-1, -1);
  }, []);

  return (
    <div className={styles.root}>
      <textarea
        {...restProps}
        value={value}
        maxLength={maxLength}
        className={classNames(className, "textarea")}
        ref={textRef}
      />
      <div className="count">
        {value?.length || 0}/{maxLength}
      </div>
    </div>
  );
}
